<?php

/**
 * Admin_Acl File
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Admin_Home
 * @package  Admin_Home_Package
 * @author Johari <joharijumali@gmail.com>  
 */

use Admin\Libraries\Sneakpeak,
	Admin\Libraries\Terminal;

use Admin\Models\Monitor\Server,
    Admin\Models\Monitor\Memory,
    Admin\Models\Monitor\Cpu,
    Admin\Models\Monitor\Proc,
    Admin\Models\Monitor\Capacity,
    Admin\Models\Monitor\Hdd,
	Admin\Models\Monitor\Load;

/**
 * Admin_Home class
 * 
 * Class description
 *
 * @category Admin_Home_Controller
 * @package  Admin_Home_Package
 * @author Johari <joharijumali@gmail.com>   
 */

class Admin_Home_Controller extends Admin_Base_Controller
{

	/**
     * Function getDashboard
	 *
	 * @return getDashboard.
     */

    public function get_dashboard()
	{

    	// Sneakpeak::initsequence();

    	return View::make('admin::dashboard.dashboard');

    }


    /**
     * Function getExtractor
     *
     * @return json extractor.
     */

    public function get_serviceState(){

        $services = Config::get('admin::service.services');
        
        $server = Server::all();

        foreach ($server as $content) {

            $serviceStatus[$content->server_id] = array();
            # Process all services montiored.
            foreach( $services as $serviceName => $servicePort )
            {
                # Determine Port Status.
                $status = @fsockopen($content->server_ip, $servicePort, $errno, $errstr, 5);
                
                # What is the result.
                if( !$status ){
                    $statusText = '<span style="color:#b94a48">Down</span><br><small>'.$errstr.'</small>';
                }else{
                    $statusText = '<span style="color:#468847"><strong>Up</strong></span>';
                }

                # Set the status.
                array_push($serviceStatus[$content->server_id], array(
                    'serviceName' => '<strong>'.$serviceName.'</strong>', 
                    // 'port' => '<strong>'.$servicePort.'</strong>' , 
                    'statusText' => $statusText));
            }

        }

        return json_encode($serviceStatus);


    }


    /**
     * Function getExtractor
     *
     * @return json extractor.
     */

    public function get_extractor()
    {
        $mainData = array();
        $server = Server::all();

        foreach ($server as $content) {

            $serverid = $content->server_id;
            $serverdesc = " <code>".strtoupper($content->server_name)."&nbsp;[ ".$content->server_ip." ]</code>";

            $maindata[$serverid]['name'] = $serverdesc;
            $maindata[$serverid]['load'] = Load::grabSingleLoad($serverid);
            $maindata[$serverid]['mem'] = Memory::grabSingleMemory($serverid);
            $maindata[$serverid]['cpu'] = Cpu::grabSingleCpu($serverid);
            $maindata[$serverid]['proc'] = Proc::grabSingleProc($serverid);
            $maindata[$serverid]['diskcap'] = Capacity::grabSingleCapacity($serverid);
            $maindata[$serverid]['hdd'] = Hdd::grabSingleHdd($serverid);
        }


        return json_encode($maindata);

    }

    /**
     * Function grabLoad
     *
     * @return json grabLoad.
     */

    public function get_grabLoad()
    {
        return json_encode(Load::grabAllLoad());
    }

    /**
     * Function grabMemory
     *
     * @return json get_grabMemory.
     */

    public function get_grabMemory()
    {
        return json_encode(Memory::grabAllMemory());
    }

    /**
     * Function get_grabCpu
     *
     * @return json get_grabCpu.
     */

    public function get_grabCpu()
    {
        return json_encode(Cpu::grabAllCpu());
    }

    /**
     * Function get_grabProc
     *
     * @return json get_grabProc.
     */

    public function get_grabProc()
    {
        return json_encode(Proc::grabAllProc());
    }

    /**
     * Function get_grabCapacity
     *
     * @return json get_grabCapacity.
     */

    public function get_grabCapacity()
    {
        return json_encode(Capacity::grabAllCapacity());
    }

    /**
     * Function get_grabAllHdd
     *
     * @return json get_grabAllHdd.
     */

    public function get_grabHdd()
    {
        return json_encode(Hdd::grabAllHdd());
    }




}

?>